//
//  LOadImageView.m
//  wulihan
//
//  Created by tang on 16/3/28.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "LOadImageView.h"
#import "Header.h"


@interface LOadImageView ()
{
    UIImageView *backIMageVIew;
    UIScrollView *mainScrollview;
    float scaleFloat;
    
    UIImageView *boderImageView;
    
}

@end


@implementation LOadImageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        scaleFloat = 0.0;
        
        [self setUserInteractionEnabled:YES];
        [self initUI];
        
        boderImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [boderImageView.layer setBorderWidth:1.0f];
        [boderImageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
        
        [self addSubview:boderImageView];
    }
    return self;
}


- (void)initUI
{
    mainScrollview = [[UIScrollView alloc] initWithFrame:self.bounds];
    [mainScrollview setBackgroundColor:uicolor(62, 62, 62)];
    [mainScrollview setUserInteractionEnabled:YES];
//    [mainScrollview setContentSize:CGSizeMake(self.bounds.size.width*2, self.bounds.size.height*2)];
    [self addSubview:mainScrollview];
    
    backIMageVIew = [[UIImageView alloc] initWithFrame:self.bounds];
    [backIMageVIew setImage:[UIImage imageNamed:@"firstttt.jpg"]];
    [backIMageVIew setContentMode:UIViewContentModeScaleAspectFit];
    [backIMageVIew setUserInteractionEnabled:YES];
    [mainScrollview addSubview:backIMageVIew];
    
//    UIRotationGestureRecognizer *twoFingersRotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)];
//    [backIMageVIew addGestureRecognizer:twoFingersRotate];
    
    
    
    UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)];
    [backIMageVIew addGestureRecognizer:twoFingerPinch];

}


- (void)setImageData:(NSData *)imageData
{
    [backIMageVIew setImage:[UIImage imageWithData:_imageData]];
}




//旋转
- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
    // Convert the radian value to show the degree of rotation
    NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
    [backIMageVIew setTransform:CGAffineTransformMakeRotation([recognizer rotation])];
}


//放大缩小
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
    if(recognizer.state== UIGestureRecognizerStateBegan) {
        scaleFloat = 1.0;
    }
    
    CGFloat scale = 1.0 - (scaleFloat - [recognizer scale]);
    
    CGAffineTransform currentTransform = backIMageVIew.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    [backIMageVIew setTransform:newTransform];
    
    [mainScrollview setContentSize:CGSizeMake(self.bounds.size.width*newTransform.a, self.bounds.size.height*newTransform.b)];
    [backIMageVIew setCenter:CGPointMake(mainScrollview.contentSize.width/2.0, mainScrollview.contentSize.height/2.0)];
    
    NSLog(@"%f", CGRectGetWidth(mainScrollview.bounds));
    
    scaleFloat = [recognizer scale];
    
//    NSLog(@"%f           %f", CGRectGetMinX(backIMageVIew.frame), CGRectGetMaxX(backIMageVIew.frame));
}






//设置取景图
- (void)setCroperInt:(int)croperInt
{
    if (croperInt == 1)         //1：1
    {
        [boderImageView setFrame:CGRectMake(0, 0, WithView-18.0, WithView-18.0)];
        [boderImageView setCenter:CGPointMake(CGRectGetMidX(mainScrollview.frame), CGRectGetMidY(mainScrollview.frame))];
    }
    else if (croperInt == 2)
    {
        
    }
    else if (croperInt == 3)
    {
        
    }
    else if (croperInt == 4)
    {
        
    }
    else if (croperInt == 5)
    {
        
    }

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
